package org.jivesoftware.spark.crowd.packet;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.spark.crowd.Param;
import org.xmlpull.v1.XmlPullParser;


public class DiscoverMembers extends IQ{
	private final List<MemberItem> members = new CopyOnWriteArrayList<MemberItem>();
	private String crowdName = "";
	@Override
	public String getChildElementXML() {
		StringBuilder buf = new StringBuilder();
		buf.append("<crowd xmlns=\"").append(Param.discoCrowdMembers_NS).append("\" crowdname=\"")
		.append(crowdName)
		.append("\">");
		synchronized (members) {
			for (MemberItem item : members) {
				buf.append(item.toXML());
			}
		}
		buf.append("</crowd>");
		return buf.toString();
		
	}
	


	public void addMember(MemberItem item) {
		synchronized (members) {
			members.add(item);
		}
	}

	public Iterator<DiscoverMembers.MemberItem> getMembers() {
		synchronized (members) {
			return Collections.unmodifiableList(members).iterator();
		}
	}
	public static class MemberItem{
		/**
		 * bare JID
		 */
		private String JID;
		/**
		 * nickname ,or Chinese name
		 */
		private String name;
		
		
		
		public String getJID() {
			return JID;
		}



		public void setJID(String jID) {
			JID = jID;
		}



		public String getName() {
			return name;
		}



		public void setName(String name) {
			this.name = name;
		}

		

		public String toXML() {
			StringBuilder buf = new StringBuilder();
			buf.append("<memberItem memberJID=\"").append(JID).append("\"");
			if(name != null)
				buf.append(" name=\"").append(name).append("\"");
			buf.append("/>");
			
			return buf.toString();
		}
		
	}
	public void setCrowdName(String crowdName){
		this.crowdName = crowdName;
	}
	public String getCrowdName(){
		return crowdName;
	}
	public static class DiscoverMembersProvider implements IQProvider{
		private static DiscoverMembersProvider instance;
		public static DiscoverMembersProvider getInstance(){
			if(instance == null)
				instance = new DiscoverMembersProvider();
			return instance; 
		}
		private DiscoverMembersProvider(){
			ProviderManager.getInstance().addIQProvider(Param.CROWD_SERVICE_NAME, Param.discoCrowdMembers_NS, this);
		}
		@Override
		public IQ parseIQ(XmlPullParser parser) throws Exception {
			DiscoverMembers discoverMembers = new DiscoverMembers();
			discoverMembers.setCrowdName(parser.getAttributeValue("", "crowdname"));
			boolean done = false;
			while(!done){
				int eventType = parser.next();
				 
				 if(eventType == XmlPullParser.START_TAG){
					 if(parser.getName().equals("memberItem")){
						 String memberJID = parser.getAttributeValue("", "memberJID");
						 String name = parser.getAttributeValue("","name");
						 					
						 DiscoverMembers.MemberItem item = new DiscoverMembers.MemberItem();
						 
						 item.setJID(memberJID);
						 item.setName(name);
												 
						 discoverMembers.addMember(item);
					 }
				 }else if(eventType == XmlPullParser.END_TAG){
					 if(parser.getName().equals(Param.CROWD_SERVICE_NAME))
						 done = true;
				 }
			}
			return discoverMembers;
		}
		
	}

}
